<?php
/**
 * 字段有:
 * 	id
 * 	creator 		
 * 	created 		
 * 	updator 		
 * 	updated 		
 * 	serialnum 	
 * 	customerid 
 * 	status 		
 * 	prodid 		
 * 	plannum 		
 * 	planperiod 
 * 	applynum 	
 * 	contractnum
 * 	actualnum 	
 * 	period 		
 * 	touserid 	
 * 	tofee 		
 * 	didtopay		
 * 	remark			
 * 	srctype		
 * 	srcuserid	
 * 	srcfee			
 * 	didpaysrc	
 * 	recuserid	
 * 	recfee			
 * 	didpayrec	
 * 	deleted		
 *  lastcomm
 */
//对{order } ... {/order }的解析
function smarty_block_order (&$params, $content, &$smarty, &$repeat){
   return $smarty->callblockfunc($params,$content,$smarty,$repeat, getorder);
}

/**
 * 参数：
 * 
 * @param unknown_type $params
 */
function getorder(&$params) {
	$table = "t_order";
	$tprefix = "$table.c_";
	//#region 构建sql条件语句
	$sqlwhere = "1=1 ";
	
	if(!isempty($params["serialnum"])){
		$sqlwhere .= " and {$tprefix}serialnum='$params[serialnum]' ";
	}
	
	if(!isempty($params["customerid"])){
		$sqlwhere .= " and {$tprefix}customerid='$params[customerid]' ";
	}
	
	if(!isempty($params["status"])){
		$sqlwhere .= " and {$tprefix}status='$params[status]' ";
	}
	
	if(!isempty($params["srctype"])){
		$sqlwhere .= " and {$tprefix}srctype='$params[srctype]' ";
	}
	
//	if(!isempty($params["srcuserid"])){
//		$sqlwhere .= " and {$tprefix}srcuserid='$params[srcuserid]' ";
//	}
//	
//	if(!isempty($params["touserid"])){
//		$sqlwhere .= " and {$tprefix}touserid='$params[touserid]' ";
//	}
//	
//	if(!isempty($params["recuserid"])){
//		$sqlwhere .= " and {$tprefix}recuserid='$params[recuserid]' ";
//	}
	
	if(!isempty($params["customer_name"])){
		$v = str_replace("*", "%", $params["customer_name"]);
		$sqlwhere .= " and customer.c_name like '$v' ";
	}
	
	if(!isempty($params["customer_mobile"])){
		$v = str_replace("*", "%", $params["customer_mobile"]);
		$sqlwhere .= " and customer.c_mobile like '$v' ";
	}
	
	if(!isempty($params["touser_name"])){
		$v = str_replace("*", "%", $params["touser_name"]);
		$sqlwhere .= " and touser.c_name like '$v' ";
	}
	
	if(!isempty($params["touser_mobile"])){
		$v = str_replace("*", "%", $params["touser_mobile"]);
		$sqlwhere .= " and touser.c_mobile like '$v' ";
	}
	
	if(!isempty($params["srcuser_name"])){
		$v = str_replace("*", "%", $params["srcuser_name"]);
		$sqlwhere .= " and srcuser.c_name like '$v' ";
	}
	
	if(!isempty($params["srcuser_mobile"])){
		$v = str_replace("*", "%", $params["srcuser_mobile"]);
		$sqlwhere .= " and srcuser.c_mobile like '$v' ";
	}
	
	if(!isempty($params["recuser_name"])){
		$v = str_replace("*", "%", $params["recuser_name"]);
		$sqlwhere .= " and recuser.c_name like '$v' ";
	}
	
	if(!isempty($params["recuser_mobile"])){
		$v = str_replace("*", "%", $params["recuser_mobile"]);
		$sqlwhere .= " and recuser.c_mobile like '$v' ";
	}
	//#endreg 构建sql条件语句
	
	return getcommbc("order", $params, 
		array("customer.name", "customer.mobile"
			,"prod.name"
			,"touser.name" ,"touserorg.name"
			,"srcuser.name" ,"srcuserorg.name" 
			,"recuser.name" ), 
		array("customer:percust.id=customerid", "prod:prod.id=prodid" 
			,"touser:user.id=touserid" ,"touser.orgid=touserorg:org.id"
			,"srcuser:user.id=srcuserid" ,"srcuser.orgid=srcuserorg:org.id"
			,"recuser:user.id=recuserid"), 
		$sqlwhere);
}

/*
function addorder($params){
	global $_db;
	//如果要新增客户
	if("-1"==@$params['customerid']){
		include_once ROOT_DIR.'/bc/block.percust.php';
		$percustparams = array();
		foreach($params as $k => $v){
			if(substr($k, 0, 3)=="pc_")
				$percustparams[substr($k,3)] = $v;
		}
		$cid = addpercust($percustparams);
		if($cid>=1){
			$params['customerid'] = $cid;
		}
		else{
			echo "新增客户失败";
			return BEND_RENDERED;
		}
	}
	//生成订单编号
	$serialnum = $_SESSION['userid']."-";
	$serialnum .= date("ymd-");//年月日
	$curmax = $_db->get1stcolval("select count(*) from t_order where c_created>=".parsedate(date("Y-m-d"), 'Y-m-d'));
	$curmax ++;
	$serialnum .= $curmax;//毫秒
	$serialnum .= rand(0, 9);
	
	return $_db->insert("order", array("serialnum"=>$serialnum, "customerid"=>@$params['customerid'], "status"=>@$params['status'], "prodid"=>@$params['prodid'], "plannum"=>@$params['plannum'], "planperiod"=>@$params['planperiod'], "applynum"=>@$params['applynum'], "contractnum"=>@$params['contractnum'], "actualnum"=>@$params['actualnum'], "period"=>@$params['period'], "touserid"=>@$params['touserid'], "tofee"=>@$params['tofee'], "didtopay"=>@$params['didtopay'], "remark"=>@$params['remark'], "srctype"=>@$params['srctype'], "srcuserid"=>@$params['srcuserid'], "srcfee"=>@$params['srcfee'], "didpaysrc"=>@$params['didpaysrc'], "recuserid"=>@$params['recuserid'], "recfee"=>@$params['recfee'], "didpayrec"=>@$params['didpayrec'], "lastcomm"=>parsedate(@$params['lastcomm']), "nextcomm"=>parsedate(@$params['nextcomm']), "commrecord"=>@$params['commrecord'], "evalcustomer"=>@$params['evalcustomer'], "toremark"=>@$params['toremark'], "srcremark"=>@$params['srcremark'], "recremark"=>@$params['recremark']));
}
//*/
?>